import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {addComments, delComments, findAllComments, getComments, updateComments} from "@/api/comments";

export const CommentsStore = defineStore('comments', {
    state:()=>{
        return {
            // 列表
            commentsList: [],
            // 详细信息
            commentsInfo: {}
        }
    },
    actions:{
        addComments(){
            return new Promise((resolve, reject)=>{
                addComments(this.commentsInfo).then(res => {
                    resolve(res.result)
                    this.commentsInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCommentsList(query) {
            return new Promise((resolve, reject)=>{
                findAllComments(query).then(res => {
                    this.commentsList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getCommentsInfo(id) {
            const {result} = await getComments(id)
            this.commentsInfo = result
            this.commentsInfo.comments_price = parseFloat(this.commentsInfo.comments_price)
        },
        updateComments(id,data) {
            return new Promise(async (resolve, reject) => {
                updateComments(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteComments(id) {
            const {code} = await delComments(id)
        },
    }
})